package com.shundr.auth.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;

import com.shundr.auth.base.BaseActivity;
import com.shundr.auth.channel.R;
import com.shundr.auth.common.view.SharedPrefUtil;
import com.shundr.auth.frame.constant.LConstants;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.util.StringUtil;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.auth.user.http.UserRequest;

public class ChangePasswordActivity extends BaseActivity {
	
	private EditText etLastPwd;
	private EditText etNewPwd;
	private EditText etRNewPwd;
	
	private String phone = "";
	private SharedPrefUtil shPreUtil;
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			
			switch (msg.what) {
			case UserRequest.CHANGE_PWD_SUCCESSED:
				ToastHelper.toastShort(mContext, "修改密码成功！");
				
				ChangePasswordActivity.this.finish();
				
				break;

			default:
				
				ToastHelper.toastShort(mContext, "修改密码失败：" + msg.obj.toString());
				
				break;
			}
			
			super.handleMessage(msg);
			
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_pwd);
		
		shPreUtil = new SharedPrefUtil(mContext, LConstants.PRE_NAME);
		
		phone = shPreUtil.getString(LConstants.STAFF_PHONE, "");
		
		etLastPwd = (EditText) findViewById(R.id.et_last_pwd);
		etNewPwd = (EditText) findViewById(R.id.et_account);
		etRNewPwd = (EditText) findViewById(R.id.et_account_confirm);
		
		findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (check()) {
					new UserRequest(mContext, mHandler).changeUserPassword(phone, 
							etLastPwd.getText().toString().trim(), 
							etNewPwd.getText().toString().trim());
				}
			}
		});
	}
	
	private boolean check() {
		
		if (StringUtil.isNull(etLastPwd.getText().toString())) {
			ToastHelper.toastShort(mContext, "请输入原密码！");
			etLastPwd.requestFocus();
			return false;
		}
		
		String newPwd = etNewPwd.getText().toString();
		
		if (StringUtil.isNull(newPwd) || newPwd.length() < 6) {
			ToastHelper.toastShort(mContext, "请重新输入新密码，密码长度最小6位！");
			etNewPwd.requestFocus();
			return false;
		}
		
		String rNewPwd = etRNewPwd.getText().toString();
		if (StringUtil.isNull(rNewPwd)) {
			ToastHelper.toastShort(mContext, "请再次输入新密码！");
			etRNewPwd.requestFocus();
			return false;
		}
		
		if (!newPwd.equals(rNewPwd)) {
			ToastHelper.toastShort(mContext, "两次输入的新密码不一致！");
			return false;
		}
		
		return true;
	}

}
